For some time, the technique of using the runtime performance of a program to guide optimizations has been discussed in the literature and occasionally implemented in production compilers. This technique is generally referred to as “profile-based optimization.” The goal is to identify those paths through a program which are executed the most frequently, and to apply more aggressive optimization techniques to those critical sections of code to make them execute faster.
In an embedded environment, not only is speed important, but the size of the generated code is often a critical cost issue. Therefore, it is necessary to balance the concerns of speed versus space. Simply identifying the time-critical code and applying aggressive optimization to it is not enough. We must optimize part of the code to be fast, and part of the code to be small, while constraining the total program size so that it fits in the available target memory. The goal is to be able to speed optimize as much of the critical part of the program as possible without violating the size constraint.
It is a generally observed phenomenon that a large portion of the execution time of a program is generally spent in a small portion of the actual code. This is what makes profile-based optimization effective: it allows you to predict which parts of the code are speed critical, and to selectively apply more aggressive speed or size optimization.
Compiling for speed on a Very Long Instruction. Word (VLIW) architecture potentially leads to significant code size growth. Compiling for size on a VLIW results in significant speed degradation. If we simply compile everything for speed, the code may grow beyond what is available on the target memory. If we simply compile for space, the resulting performance is unacceptable.
Compiler optimization control flags generally work on a whole source module. The typical development scenario is to compile, run, and profile a program, then recompile modules based on the feedback provided by the profiler: compile some modules for speed, some for size.